home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / Akua Sweets 131 / Akua Sweets Examples / File / Search & Replace < prev    next >
Encoding:
Text File  |  1999-03-04  |  6.3 KB  |  269 lines  |  [TEXT/ToyS]

  1. -- Properties
  2. property kasPrefName : "Search & Replace V1.0"
  3. property kasDoOnlyText : true
  4.  
  5. -- Globals
  6. global gasInfoWind -- Info window
  7. global gasInfoPos -- Position of info window
  8.  
  9. global gasFoldersToDo -- The folders left to process
  10. global gasChecked -- Number checked!
  11. global gasSaved -- Number changed
  12.  
  13. global gasSearch -- Search string
  14. global gasReplace -- Replace with
  15. global gasDoRsrc, gasDoData
  16.  
  17.  
  18. on open fsObjs
  19.     -- Load prefs, show window
  20.     pfLoad()
  21.     
  22.     -- Set up prefix
  23.     set gasChecked to 0
  24.     set gasSaved to 0
  25.     
  26.     GetInputs()
  27.     
  28.     set gasInfoWind to display info titled kasPrefName ¬
  29.         located at gasInfoPos ¬
  30.         message "Scanning…"
  31.     
  32.     display info gasInfoWind ¬
  33.         message ("Search: " & gasSearch) ¬
  34.         at line 8
  35.     
  36.     display info gasInfoWind ¬
  37.         message ("Replace: " & gasReplace) ¬
  38.         at line 9
  39.     
  40.     -- Do files
  41.     set gasFoldersToDo to {}
  42.     
  43.     repeat with fsObj in fsObjs
  44.         set myInfo to (basic info for fsObj)
  45.         
  46.         if (system type of myInfo is "fold") then
  47.             set gasFoldersToDo to gasFoldersToDo & {fsObj}
  48.         else
  49.             DoOne(fsObj)
  50.         end if
  51.     end repeat
  52.     
  53.     -- Do folders
  54.     repeat while gasFoldersToDo is not {}
  55.         -- Pop one off the end
  56.         set n to the number of items of gasFoldersToDo
  57.         set fsObj to item n of gasFoldersToDo
  58.         
  59.         if (n > 1) then
  60.             set gasFoldersToDo to items 1 through (n - 1) of gasFoldersToDo
  61.         else
  62.             set gasFoldersToDo to {}
  63.         end if
  64.         
  65.         display info gasInfoWind ¬
  66.             message ("Folders to go: " & n) ¬
  67.             at line 6 ¬
  68.             using color (15 * 32)
  69.         
  70.         -- Process it
  71.         GoDeep(fsObj)
  72.     end repeat
  73.     
  74.     display info gasInfoWind message "DONE!"
  75.     
  76.     pause for 3 with seconds timing -- Let screen wait...
  77.     
  78.     set gasInfoPos to screen location of ¬
  79.         (display info gasInfoWind with disposal)
  80.     
  81.     pfSave() -- Save window location
  82. end open
  83.  
  84.  
  85. on GetInputs()
  86.     set gasSearch to GetString("search string", gasSearch)
  87.     set gasReplace to GetString("replace string", gasReplace)
  88.     
  89.     if (length of gasReplace) is not (length of gasSearch) then ¬
  90.         if (ShowChoice("CAUTION:" & return & return & "Search & replace are not the same length", ¬
  91.             {"Cancel", "I know what I'm doing!", "Padded Pascal"}) is "Padded Pascal") then
  92.             -- Make pascal string
  93.             set n to length of gasReplace
  94.             set gasReplace to (ASCII character n) & gasReplace
  95.             set n to n + 1
  96.             
  97.             -- Make pascal string
  98.             set m to length of gasSearch
  99.             set gasSearch to (ASCII character m) & gasSearch
  100.             set m to m + 1
  101.             
  102.             -- Pad them to same length
  103.             if (n > m) then set m to n
  104.             repeat while (length of gasReplace) < m
  105.                 set gasReplace to gasReplace & (ASCII character 0)
  106.             end repeat
  107.             repeat while (length of gasSearch) < m
  108.                 set gasSearch to gasSearch & (ASCII character 0)
  109.             end repeat
  110.         end if
  111.     
  112.     set choice to ShowChoice("Replace in Data, Resource or Both forks?", ¬
  113.         {"Data", "Rsrc", "Both"})
  114.     
  115.     set gasDoData to (choice is "Data") or (choice is "Both")
  116.     set gasDoRsrc to (choice is "Rsrc") or (choice is "Both")
  117. end GetInputs
  118.  
  119.  
  120. on GetString(sName, sDef)
  121.     return text returned of ¬
  122.         (display dialog ("Enter the " & sName & ":") ¬
  123.             buttons {"Cancel", "OK"} ¬
  124.             default answer sDef ¬
  125.             default button 2 ¬
  126.             with icon note)
  127. end GetString
  128.  
  129.  
  130. on ShowAlert(msg)
  131.     return button returned of ¬
  132.         (display dialog msg ¬
  133.             buttons {"Cancel", "Yo!"} ¬
  134.             default button 2 ¬
  135.             with icon stop)
  136. end ShowAlert
  137.  
  138.  
  139. on ShowChoice(msg, choices)
  140.     return button returned of ¬
  141.         (display dialog msg ¬
  142.             buttons choices ¬
  143.             default button (number of items of choices) ¬
  144.             with icon caution)
  145. end ShowChoice
  146.  
  147.  
  148. on DoOne(fsObj)
  149.     set fInfo to (basic info for fsObj)
  150.     set fname to (catalog name of fInfo)
  151.     
  152.     set gasChecked to gasChecked + 1
  153.     
  154.     display info gasInfoWind ¬
  155.         message fname ¬
  156.         at line 2
  157.     
  158.     if (gasDoData and (data fork length of fInfo > 0)) then
  159.         set fileData to read data from the data fork of fsObj
  160.         
  161.         display info gasInfoWind ¬
  162.             message ("Checking Data: " & (data fork length of fInfo)) ¬
  163.             at line 3
  164.         
  165.         set newData to munge fileData ¬
  166.             searching for gasSearch ¬
  167.             replacing it with gasReplace
  168.         
  169.         if (not (the same data is in fileData as in newData)) then
  170.             set gasSaved to gasSaved + 0.5
  171.             write data to the data fork of fsObj ¬
  172.                 from buffer newData
  173.             display info gasInfoWind ¬
  174.                 message ("Saved Data") ¬
  175.                 at line 3
  176.         end if
  177.     end if
  178.     
  179.     if (gasDoRsrc and (resource fork length of fInfo > 0)) then
  180.         display info gasInfoWind ¬
  181.             message ("Checking Resource: " & (resource fork length of fInfo)) ¬
  182.             at line 4
  183.         set fileRef to ¬
  184.             open fork from fsObj ¬
  185.                 with resource and write access
  186.         
  187.         set fileData to read data from fileRef
  188.         
  189.         set newData to munge fileData ¬
  190.             searching for gasSearch ¬
  191.             replacing it with gasReplace
  192.         
  193.         if (not (the same data is in fileData as in newData)) then
  194.             if (the data length of newData) is not (the data length of fileData) then ¬
  195.                 ShowAlert("Please cancel, you will corrupt this file!")
  196.             set gasSaved to gasSaved + 0.5
  197.             size fork fileRef given «class len »:0
  198.             write data to fileRef ¬
  199.                 from buffer newData
  200.             
  201.             display info gasInfoWind ¬
  202.                 message ("Saved Resources") ¬
  203.                 at line 4
  204.         end if
  205.         
  206.         close fork fileRef
  207.     end if
  208. end DoOne
  209.  
  210.  
  211. on GoDeep(foldObj)
  212.     display info gasInfoWind ¬
  213.         message "Path: " & (foldObj as string)
  214.     
  215.     set daddy to foldObj as string
  216.     
  217.     -- Do kinds that match
  218.     display info gasInfoWind ¬
  219.         message "Scanning files" at line 5
  220.     
  221.     if (kasDoOnlyText) then
  222.         set myItems to the entries in foldObj ¬
  223.             whose kinds are a file ¬
  224.             whose types are in {"TEXT"}
  225.     else
  226.         set myItems to the entries in foldObj ¬
  227.             whose kinds are a file
  228.     end if
  229.     
  230.     repeat with myItem in myItems
  231.         DoOne((daddy & myItem) as alias)
  232.     end repeat
  233.     
  234.     -- Do folders
  235.     display info gasInfoWind ¬
  236.         message "Scanning subfolders" at line 5
  237.     
  238.     set myItems to the entries in foldObj ¬
  239.         whose kinds are a folder
  240.     
  241.     repeat with myItem in myItems
  242.         set gasFoldersToDo to gasFoldersToDo & {(daddy & myItem) as alias}
  243.     end repeat
  244.     
  245.     -- Done
  246.     display info gasInfoWind ¬
  247.         message "…" at line 5
  248. end GoDeep
  249.  
  250.  
  251. on pfLoad()
  252.     try
  253.         set ourPrefs to (load preference named kasPrefName)
  254.     on error
  255.         set ourPrefs to {{8, 48}, ">LORE<", "◊LIFE◊", true, true}
  256.     end try
  257.     
  258.     set gasInfoPos to item 1 of ourPrefs
  259.     set gasSearch to item 2 of ourPrefs
  260.     set gasReplace to item 3 of ourPrefs
  261.     set gasDoData to item 4 of ourPrefs
  262.     set gasDoRsrc to item 5 of ourPrefs
  263. end pfLoad
  264.  
  265.  
  266. on pfSave()
  267.     save preference {gasInfoPos, gasSearch, gasReplace, gasDoData, gasDoRsrc} named kasPrefName
  268. end pfSave
  269.